 <template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">控制台</el-breadcrumb-item>
      <el-breadcrumb-item><b>业主信息管理</b></el-breadcrumb-item>
      <el-breadcrumb-item>车辆管理</el-breadcrumb-item>
    </el-breadcrumb>
    <hr>

    <el-card style="font-size: 20px;font-family: 'Helvetica Nee',serif">
      <b>车辆管理</b>
      <p style="font-size: 15px">用于管理小区里的车辆信息，包括业主、家庭成员及租户的车辆</p>
    </el-card>

    <el-card class="box-card">
      <div class="card-header">
        <el-row>
          <!-- 添加按钮 -->
          <el-col :span="18">
            <Button type="success" icon="md-add" @click="dialogFormVisibleAdd = true">添加</Button>
            <span style="margin-left: 10px;"></span>
            <Button v-if="showDeleteButton" type="error" icon="md-trash" @click="deleteByIds()">批量删除</Button>
          </el-col>
          <!-- 搜索框 -->
          <el-col :span="4">
            <Input search style="width: 260px;" enter-button placeholder="请输入搜索的内容" @on-search="search()"/>
          </el-col>
          <!-- 根据小区查询下拉框 -->
          <el-col :span="2" style="text-align: right;">
            <Dropdown @on-click="selectByCommunity">
                <Button type="primary">
                  根据小区查询 <Icon type="ios-arrow-down"></Icon>
                </Button>
                <DropdownMenu slot="list">
                    <DropdownItem :name="null">
                      所有小区
                    </DropdownItem>
                    <DropdownItem v-for="(community, index) in communityList" :key="index" :name="community.code">
                      {{ community.name }}
                    </DropdownItem>
                </DropdownMenu>
            </Dropdown>
          </el-col>
        </el-row>
      </div>

      <el-table v-loading="loadTable" :border="true" ref="multipleTable" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column fixed prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="communityName" label="所属小区" width="180"></el-table-column>
        <el-table-column prop="carImage" label="车辆照片" width="120" align="center">
          <template slot-scope="scope">
            <viewer :images="[scope.row.carImage]" style="height: 50px">
              <img :src="scope.row.carImage" height="100%" width="100%" alt="">
            </viewer>
          </template>
        </el-table-column>
        <el-table-column prop="peopleName" label="所属人" width="150"></el-table-column>
        <el-table-column prop="carName" label="车辆名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="carColor" label="车辆颜色" width="120"></el-table-column>
        <el-table-column prop="licensePlateNumber" label="车辆牌照" width="150"></el-table-column>
        <el-table-column prop="describes" label="备注" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="gmtCreate" label="创建时间" width="180"></el-table-column>
        <el-table-column align="center" label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <ButtonGroup>
              <Button type="info" icon="md-create" @click="showUpdateDialog(scope.row)"></Button>
              <Button type="error" icon="md-trash" @click="deleteIds = [scope.row.id]; deleteByIds()"></Button>
            </ButtonGroup>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <Page :total="total" show-elevator show-sizer :current="currentPage" :page-size="pageSize" @on-change="changePage" @on-page-size-change="changePageSize"/>
      </div>
    </el-card>

    <!-- 编辑弹窗 -->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="dialogFormVisibleUpdate" center>
      <el-form :model="editFormUpdate">
          <el-form-item label="所属小区" required>
              <el-select v-model="editFormUpdate.communityCode" placeholder="请选择" border>
                <el-option v-for="(item, index) in communityList" :key="index" :label="item.name" :value="item.code"></el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="车辆名称" required>
            <el-input v-model="editFormUpdate.carName"></el-input>
          </el-form-item>
          <el-form-item label="车辆照片" required>
            <el-upload accept="image/*" :file-list="fileList" :on-remove="handleRemove" :on-exceed="handleExceed" :disabled="uploadDisabled" action="#" :http-request="uploadImage" :limit="1">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
          <el-form-item label="车辆颜色" required>
            <el-input v-model="editFormUpdate.carColor"></el-input>
          </el-form-item>
          <el-form-item label="车辆牌照" required>
            <el-input v-model="editFormUpdate.licensePlateNumber"></el-input>
          </el-form-item>
          <el-form-item label="所属人" required>
            <el-select v-model="editFormUpdate.peopleId" placeholder="请选择" border>
              <el-option v-for="(item, index) in peopleList" :key="index" :label="item.peopleName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述" required>
            <el-input type="textarea" :rows="2" v-model="editFormUpdate.describes"></el-input>
          </el-form-item>
          <el-form-item center>
            <el-button type="primary" @click="update()" :disabled="uploadDisabled">确 定</el-button>
            <el-button @click="dialogFormVisibleUpdate = false" :disabled="uploadDisabled">关 闭</el-button>
          </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 添加弹窗 -->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="dialogFormVisibleAdd" center>
      <el-form :model="editFormAdd">
        <el-form-item label="所属小区" required>
              <el-select v-model="editFormAdd.communityCode" placeholder="请选择" border>
                <el-option v-for="item, index in communityList" :key="index" :label="item.name" :value="item.code">
                </el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="车辆名称" required>
            <el-input v-model="editFormAdd.carName"></el-input>
          </el-form-item>
          <el-form-item label="车辆照片" required>
            <el-upload :file-list="fileList" :on-exceed="handleExceed" :disabled="uploadDisabled" action="#" :http-request="uploadImage" :limit="1" list-type="picture-card">
              <i class="el-icon-plus"></i>
              <div slot="file" slot-scope="{file}">
                <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
                <span class="el-upload-list__item-actions">
                  <span class="el-upload-list__item-delete" @click="handleRemove(file)">
                    <i class="el-icon-delete"></i>
                  </span>
                </span>
              </div>
            </el-upload>
          </el-form-item>
          <el-form-item label="车辆颜色" required>
            <el-input v-model="editFormAdd.carColor"></el-input>
          </el-form-item>
          <el-form-item label="车辆牌照" required>
            <el-input v-model="editFormAdd.licensePlateNumber"></el-input>
          </el-form-item>
          <el-form-item label="所属人" required>
            <el-select v-model="editFormUpdate.peopleId" placeholder="请选择" border>
              <el-option v-for="(item, index) in peopleList" :key="index" :label="item.peopleName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述" required>
            <el-input type="textarea" :rows="2" v-model="editFormAdd.describes"></el-input>
          </el-form-item>
          <el-form-item center>
            <el-button type="primary" @click="add()" :disabled="uploadDisabled">确 定</el-button>
            <el-button @click="dialogFormVisibleAdd = false" :disabled="uploadDisabled">关 闭</el-button>
          </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 返回顶部按钮 -->
    <BackTop></BackTop>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "vehicle",
  data() {
    return {
      uploadImageId: "",
      fileList: [],
      dialogImageUrl: '',
      uploadDisabled: false,
      loadTable: false,
      showDeleteButton: false,
      deleteIds: [],
      editFormUpdate: {},
      editFormAdd: {},
      dialogFormVisibleUpdate: false,
      dialogFormVisibleAdd: false,
      communityList: [],
      communityCode: null,
      peopleList: [],
      searchText: "",
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    search() {

    },
    uploadImage(data) {      
      let filename = data.file.name;
      if (filename == this.editFormAdd.carImage || filename == this.editFormUpdate.carImage) {
        this.$message.warning("未更改图片！");
        this.fileList = [];
        return;
      }
      this.uploadDisabled = true;
      let fileData = data.file;
      let file = new this.AV.File(filename, fileData);      
      file.save().then(
      (file) => {
        this.uploadImageId = file.id;
        this.editFormUpdate.carImage = file.attributes.url;
        this.editFormAdd.carImage = file.attributes.url;
        this.$message.success("上传成功！");
        this.uploadDisabled = false;
      },
      (error) => {
        this.$message.error(error);
        this.uploadDisabled = false;
      });      
    },
    handleRemove() {
      this.uploadDisabled = true;
      this.fileList = [];
      this.AV.File.createWithoutData(this.uploadImageId).destroy();
      this.$message.success("已删除！");
      this.uploadDisabled = false;
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件！`);
    },
    showUpdateDialog(row) {
      this.dialogFormVisibleUpdate = true;
      this.editFormUpdate = row;
    },
    changePage(pageNum) {
      this.currentPage = pageNum;
      this.loadDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.loadDataList();
    },
    handleSelectionChange(rows) {
      if (rows.length === 0) {
        this.showDeleteButton = false;
        return;
      }
      this.showDeleteButton = true;
      for (let item of rows) {
        this.deleteIds.push(item.id);
      }
    },
    selectByCommunity(communityCode) {
      this.communityCode = communityCode;
      this.pageSize = 10;
      this.currentPage = 1;
      this.loadDataList();
    },
    update() {
      if (Object.keys(this.editFormUpdate).length === 0) {
        this.$message.error("请填写内容！");
        return;
      }
      let url = this.host + "/vehicle/update";
      this.axios.post(url, JSON.stringify(this.editFormUpdate), {
        headers: {
          "Content-Type": "application/json"
        }}).then((response) => {
          if (response.data.statusCode == 2000) {
            this.$message.success(response.data.data);
            this.dialogFormVisibleUpdate = false;
            this.loadDataList();
          } else {
            this.$message.error(response.data.message);
          }
      });
    },
    add() {
      if (Object.keys(this.editFormAdd).length === 0) {
        this.$message.error("请填写内容！");
        return;
      }
      let url = this.host + "/vehicle/add";
      this.axios.post(url, JSON.stringify(this.editFormAdd), {
        headers: {
          "Content-Type": "application/json"
        }}).then((response) => {
          if (response.data.statusCode == 2000) {
            this.$message.success(response.data.data);
            this.dialogFormVisibleAdd = false;
            this.loadDataList();
          } else {
            this.$message.error(response.data.message);
          }
      });
    },
    deleteByIds() {
      this.loadTable = true;
      let url = this.host + "/vehicle/delete?" + this.deleteIds.map(item => `ids=${encodeURIComponent(item)}`).join('&');
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.$message.success(response.data.data);
          this.loadDataList();          
        } else {
          this.$message.error("删除失败，请稍后再试！");          
        }
        this.loadTable = false;
        this.deleteIds = [];
        this.showDeleteButton = false;
      });      
    },
    loadDataList() {
      this.fileList = [];
      this.loadTable = true;
      let url = this.host + "/vehicle/list?pageNum=" + this.currentPage + "&pageSize=" + this.pageSize;
      if (this.communityCode != null) {
        url = this.host + "/vehicle/list?pageNum=" + this.currentPage + "&pageSize=" + this.pageSize + "&communityCode=" + this.communityCode;
      }
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.tableData = response.data.data.records;
          this.currentPage = response.data.data.current;
          this.pageSize = response.data.data.size;
          this.total = response.data.data.total;          
        } else {
          this.$message.error("加载数据出错了！");
        }
        this.loadTable = false;
      });      
    },
    loadCommunityList() {
      let url = this.host + "/community/list";
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.communityList = response.data.data.records;
        } else {
          this.$message.error("加载数据出错了！");
        }
      });
    },
    loadPeopleList() {
      let url = this.host + "/owners/list";
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.peopleList = response.data.data.records;
        } else {
          this.$message.error("加载数据出错了！");
        }
      });
    }
  },
  mounted() {
    this.loadPeopleList();
    this.loadCommunityList();
    this.loadDataList();
  }
}
</script>

<style scoped>
.card-header {
  margin-bottom: 20px;
}

.block {
  text-align: center;
  margin-top: 10px;
}

.el-select {
  margin-left: 15px;
  margin-right: 30px;
}

.el-date-picker {
  margin-left: 50px;
}
.sou{
  display: inline;
}

</style>
